package com.example.lpactivity.fragment.news;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.lpactivity.R;

import java.util.ArrayList;

public class NewsTitleFragment extends Fragment {
    private boolean isLandView = false;

    private ArrayList<News> newsDatas = new ArrayList<>();

    private View view;
    private RecyclerView recyclerView;

    public void setLandView(boolean landView) {
        isLandView = landView;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.news_title_fragment, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        recyclerView = view.findViewById(R.id.rv_title);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
        recyclerView.setAdapter(new NewsAdapter());
    }

    private void initData() {
        int i = 20;
        for (int i1 = 0; i1 < i; i1++) {
            newsDatas.add(new News("" + i1, "" + System.currentTimeMillis()));
        }
    }

    private class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHodler> {

        @NonNull
        @Override
        public ViewHodler onCreateViewHolder(@NonNull final ViewGroup viewGroup, final int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.new_title__item, viewGroup, false);
            final NewsAdapter.ViewHodler viewHolder = new ViewHodler(view);

            viewHolder.tv_title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isLandView) {
                        NewsContentFragment fragment = (NewsContentFragment) getActivity().getFragmentManager().findFragmentById(R.id.fm_content);
                        fragment.loadData(newsDatas.get(viewHolder.getAdapterPosition()));
                    } else {
                        Log.i(getClass().getSimpleName(), "i: " + i);
                        Log.i(getClass().getSimpleName(), "getAdapterPosition: " + viewHolder.getAdapterPosition());
                        ContentActivity.startNewsContentFragment(viewGroup.getContext(), newsDatas.get(viewHolder.getAdapterPosition()));
                    }
                }
            });
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHodler viewHodler, int i) {
            viewHodler.tv_title.setText(newsDatas.get(i).getTitle());

        }

        @Override
        public int getItemCount() {
            return newsDatas.size();
        }

        class ViewHodler extends RecyclerView.ViewHolder {

            TextView tv_title;

            public ViewHodler(@NonNull View itemView) {
                super(itemView);
                tv_title = itemView.findViewById(R.id.tv_title);
            }
        }
    }

}
